var ctx;
var level = -1;
var pId = null;

function setCtx(ctx){
	this.ctx = ctx;
	initListener();
}

$(function(){
	var menuLevel = $("#mLevel").val();
	var system = $("#system").val();
	if(system == 1 ){
		$("#menuTypeArea").show();
		$("#menuLevelArea").show();
		
		if(menuLevel > 1 ){
			$("#parentMenu").show();
			
			if(menuLevel == 2){
				parentList(1 , system);
			}else if(menuLevel == 3){
				parentList(2 , system);
			}
		}else{
			$("#parentMenu").hide();
		}
	}else if(system == -1 ){
		$("#menuTypeArea").hide();
		$("#menuLevelArea").hide();
		$("#parentMenu").hide();
	}else{
		$("#menuTypeArea").hide();
		$("#menuLevelArea").show();
		
		if(menuLevel > 1 ){
			$("#parentMenu").show();
			
			if(menuLevel == 2){
				parentList(1 , system);
			}else if(menuLevel == 3){
				parentList(2 , system);
			}
		}else{
			$("#parentMenu").hide();
		}
	}
});

//选择菜单等级
function initListener(){
	$('#menuLevel').change(function(){ 
		level = $(this).children('option:selected').val();
		
		if(level == 1){
			$("#parentMenu").hide();
			
		}else if(level == 2){
			$("#parentMenu").show();
			
			parentList(1 , $("#system").children('option:selected').val());
			
		}else if(level == 3){
			$("#parentMenu").show();
			
			parentList(2 , $("#system").children('option:selected').val());
		}else{
			$("#parentMenu").hide();
		}
	});
	
	$('#system').change(function(){ 
		var selsystem = $(this).children('option:selected').val();
		
		if(selsystem == 1 ){
			$("#menuTypeArea").show();
			$("#menuLevelArea").show();
			
			var level = $("#menuLevel").children('option:selected').val();
			if(level > 1 ){
				$("#parentMenu").show();
				
				if(level == 2){
					parentList(1 , $("#system").children('option:selected').val());
				}else if(level == 3){
					parentList(2 , $("#system").children('option:selected').val());
				}
			}else{
				$("#parentMenu").hide();
			}
		}else if(selsystem == -1 ){
			$("#menuTypeArea").hide();
			$("#menuLevelArea").hide();
			$("#parentMenu").hide();
		}else{
			$("#menuTypeArea").hide();
			$("#menuLevelArea").show();
			
			var level = $("#menuLevel").children('option:selected').val();
			if(level > 1 ){
				$("#parentMenu").show();
				
				if(level == 2){
					parentList(1 , $("#system").children('option:selected').val());
				}else if(level == 3){
					parentList(2 , $("#system").children('option:selected').val());
				}
			}else{
				$("#parentMenu").hide();
			}
		}
	});
}

	
function parentList(l , system){
	
		$("#parentMenu").show();
		$.ajax({
	        type : "GET",
	        url : ctx + "/menubutton/getByLevel",
	        cache : false,
	        dataType : 'json',
	        data : {
	        	level : l,
	        	system:system
	        },
	        success : function(data) {
	        	if (data.success) {
	        		var parentId = $("#parentId");
					parentId.empty();
					var cur = $("#mParent").val();
					for(var i=0;i<data.obj.length;i++) {
						var parent = data.obj[i];
						if(cur == parent.id){
							parentId.append("<option value='"+ parent.id + "' selected='selected'>"+ parent.menuName+"</option>");
						}else{
							parentId.append("<option value='"+ parent.id + "'>"+ parent.menuName+"</option>");
						}
					}
	            }
	        }
		});
	
}


function deleteByIds(){
	//提示框
	tip("删除成功！");
	
	//确认框
	var submit = function () {  
		//alert("确认！"); 
	};
	var mess = "是否提交以上内容！";
	var sub = "确定";
	var calcle = "返回";
	alertMess(submit,mess,sub,calcle);
	
}

//验证菜单名称
function checkMenuName(){
	var menuName = $("#menuName").val();
	//解决ie9以下placeholder与value重复的问题
	if(menuName == $("#menuName").attr("placeholder")){
		menuName = "";
	}
		if(menuName == null || menuName == ""){
			layer.tips('菜单名称不能为空!', '#menuName', {
				tips: 3,
			    time:1000
			});
			return false;
		}else{
			return true;
		}
}

//验证菜单顺序
function checkMenuOrder(){
	var reg = /^[0-9]*[1-9][0-9]*$/;
	var menuOrder = $("#menuOrder").val();
	if(menuOrder == null || menuOrder == ""){
		layer.tips('排序不能为空!', '#menuOrder', {
			tips: 3,
			time:1000
		});
		return false;
	}else if(!reg.test(menuOrder)){
		layer.tips('请输入正整数!', '#menuOrder', {
			tips: 3,
			time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证菜单等级
function checkMenuLevel(){
	var menuLevel = $("#menuLevel").val();
	//解决ie9以下placeholder与value重复的问题
	if(menuLevel == $("#menuLevel").attr("placeholder")){
		menuLevel = "";
	}
	if(menuLevel == -1){
		layer.tips('请选择菜单等级!', '#menuLevel', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证所属系统
function checkSystem(){
	var system = $("#system").val();
	//解决ie9以下placeholder与value重复的问题
	if(system == $("#system").attr("placeholder")){
		system = "";
	}
	if(system == -1){
		layer.tips('请选择所属系统!', '#system', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证菜单类型
function checkMenuType(){
	if($("#menuTypeArea").css("display") == "block"){
		var menuType = $("#menuType").val();
		//解决ie9以下placeholder与value重复的问题
		if(menuType == $("#menuType").attr("placeholder")){
			menuType = "";
		}
		if(menuType == -1){
			layer.tips('请选择菜单范围!', '#menuType', {
				tips: 3,
				time:1000
			});
			return false;
		}else{
			return true;
		}
	}else{
		return true;
	}
}

//新增或编辑菜单(1:新增 2:编辑)
var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
function saveOrUpdateMenu (){

	var id;
	if(level == -1){
		level = $("#mLevel").val();
	}
	if($("#menuId").val() != null && $("#menuId").val() != ''){
		id = $("#menuId").val();
	}
	var mName = $("#menuName").val();
	var mURL = $("#locationURL").val();
	var mOrder = $("#menuOrder").val();
	var pId;
	if(level == 1){
		pId = 0;
	}else{
		pId = $("#parentId").val();
	}
	
	var system = $("#system").val();
	var menuType = $("#menuType").val();
	if(system != 1){
		menuType = 0;
	}
	
	if(checkMenuName() && checkMenuOrder()&& checkSystem() && checkMenuType()
			&& checkMenuLevel()){
		$.post(ctx+"/menubutton/editMenu",
				{
			id:id,
			mName:mName,
			mLevel:level,
			mURL:mURL,
			mOrder:mOrder,
			mPId:pId,
			system:system,
			menuType:menuType
				},function(data){
					data = JSON.parse(data);
					if(data.success){
						layer.msg("操作成功",{
							icon: 1,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						},function(){
							parent.loadContent("/menubutton/menuButtonList");
							parent.layer.close(index);
						});
					} else {
						layer.msg(data.msg,{
							icon: 2,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						});
					}
				});
	}
}
